import os

from pathlib import Path

from dataclasses import asdict

DEFAULT_PROJECT_DIR = Path(__file__).resolve().parent.parent.parent
class PromptManager:
    """
    Manages prompt templates at agent, flow, node level. This class is dedicated to loading and rendering prompts (system prompt, user prompt).
    
    Notes: Flow and node are defined by project [PocketFlow](https://github.com/the-pocket/PocketFlow)

    Attributes:
        prompt_dir: Directory containing prompt templates.
        agent
        flow
    """

    def __init__(
        self,
        prompt_dir: str = f"{DEFAULT_PROJECT_DIR}/prompts",
    ):
        self.prompt_dir: str = prompt_dir
        print(self.prompt_dir)
    """
        self.agent_template: Template = self._load_template('system_prompt')
        self.user_template: Template = self._load_template('user_prompt')

    def _load_template(self, template_name: str) -> Template:
        if self.prompt_dir is None:
            raise ValueError('Prompt directory is not set')
        template_path = os.path.join(self.prompt_dir, f'{template_name}.j2')
        if not os.path.exists(template_path):
            raise FileNotFoundError(f'Prompt file {template_path} not found')
        with open(template_path, 'r') as file:
            return Template(file.read())
    """

    def get_system_message(self) -> str:
        return self.system_template.render().strip()
    def get_user_prompt_template(self, template_name: str) -> str:
        print(self.prompt_dir, template_name)
        template_path = os.path.join(self.prompt_dir, f'{template_name}')
        # 打开文件并读取内容
        content = ""
        with open(template_path, 'r', encoding='utf-8') as file:
            content = file.read()
        return content
    
    def get_example_user_message(self) -> str:
        """This is an initial user message that can be provided to the agent
        before *actual* user instructions are provided.

        It can be used to provide a demonstration of how the agent
        should behave in order to solve the user's task. And it may
        optionally contain some additional context about the user's task.
        These additional context will convert the current generic agent
        into a more specialized agent that is tailored to the user's task.
        """

        return self.user_template.render().strip()

   
  
